import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [{
            path: '/',
            redirect: 'home'
        },
        {
            path: '/login',
            name: 'login',
            component: () =>
                import ('@/view/Login.vue')
        },
        {
            path: '/home',
            name: 'home',
            meta: {
                title: '主页',
            },
            component: () =>
                import ('@/view/Home.vue')
        },
    ]
})

// 挂载路由导航守卫：to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to, from, next) => {
    // 放行登录页面
    if (to.path === '/login') {
        return next()
    }
    // 获取并校验token
    const tokenStr = sessionStorage.getItem('token')
    if (!tokenStr) {
        return next('/login')
    } else {
        next()
    }
})
export default router